package edu.hawaii.myisern.commandline;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

/**
 * Demonstrates that the MyIsernCommandLineInterface class is working.
 * 
 * @author Phuoc Le
 * @author Ka Yee Leung
 * @author Jared Sunouchi
 */
public class TestMyIsernCommandLineInterface {

  private String userDirectory = "user.dir";
  private File collaborationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/input.test.collaborations.example.xml");
  private File organizationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/input.test.organizations.example.xml");
  private File researcherFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/input.test.researchers.example.xml");

  private File validCollaborationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.collaborations.example.xml");
  private File validOrganizationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.organizations.example.xml");
  private File validResearcherFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.researchers.example.xml");
  
  private String moreTesting = "more testing";
  
  /**
   * Tests notValidFirstCommand method.
   */
  @Test
  public void testNotValidFirstCommand() {
    Map<String, String> test = new HashMap<String, String>();
    test.put("-test", moreTesting);
    test.put("-test1", moreTesting);
    
    assertEquals("Check for valid first command", false, 
        MyIsernCommandLineInterface.notValidFirstCommand(test, "-test"));
    assertEquals("Check for not valid first command", true, 
        MyIsernCommandLineInterface.notValidFirstCommand(test, "-testing"));
  }
  
  /**
   * Tests not valid command methods.
   */
  @Test
  public void testNotValidCommand() {
    String test = "-test";
    String test1 = "-test1";
    String testing = "-testing";
    String uniqueId = "<uniqueID>";
    
    Map<String, String> firstArg = new HashMap<String, String>();
    firstArg.put(test, moreTesting);
    firstArg.put(test1, moreTesting);
    
    Map<String, String> secondArg = new HashMap<String, String>();
    firstArg.put(test, moreTesting);
    firstArg.put(test1, moreTesting);
    
    Map<String, String> thirdArg = new HashMap<String, String>();
    thirdArg.put(test, moreTesting);
    thirdArg.put(test1, moreTesting);
    
    List<List<String>> completeCommandList = new ArrayList<List<String>>();
    List<String> commandList = new ArrayList<String>();
    commandList.add(test);
    commandList.add(test);
    commandList.add(test);    
    completeCommandList.add(commandList);
    
    commandList = new ArrayList<String>();
    commandList.add(test1);
    commandList.add(test1);
    commandList.add(uniqueId);
    completeCommandList.add(commandList);
    
    assertEquals("Check for valid first command", false, 
        MyIsernCommandLineInterface.notValidFirstCommand(firstArg, test));
    assertEquals("Check for not valid first command", true, 
        MyIsernCommandLineInterface.notValidFirstCommand(firstArg, testing));
    
    assertEquals("Check for valid second command", false, 
        MyIsernCommandLineInterface.notValidSecondCommand(secondArg, test, test, 
            completeCommandList));
    assertEquals("Check for not valid second command", true, 
        MyIsernCommandLineInterface.notValidSecondCommand(secondArg, test, testing, 
            completeCommandList));
    
    assertEquals("Check for valid third command", false, 
        MyIsernCommandLineInterface.notValidThirdCommand(thirdArg, test, test, test,
            completeCommandList));
    assertEquals("Check for not valid third command", true, 
        MyIsernCommandLineInterface.notValidThirdCommand(thirdArg, test, test, testing,
            completeCommandList));
  }
  
  /**
   * Tests normal input for command line interface.
   * 
   * @throws Exception If problems occur.
   */
  @Test
  public void testInvalidInput() throws Exception {
    MyIsernXmlLoader copier = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile,
        validResearcherFile);
    String copyCollaborationsXml = copier.getCollaborationsXml();
    String copyOrganizationsXml = copier.getOrganizationsXml();
    String copyResearchersXml = copier.getResearchersXml();

    collaborationFile.delete();
    organizationFile.delete();
    researcherFile.delete();
    collaborationFile.createNewFile();
    organizationFile.createNewFile();
    researcherFile.createNewFile();
    
    FileOutputStream fos = new FileOutputStream(collaborationFile);
    fos.write(copyCollaborationsXml.getBytes());
    fos.close();
    fos = new FileOutputStream(organizationFile);
    fos.write(copyOrganizationsXml.getBytes());
    fos.close();
    fos = new FileOutputStream(researcherFile);
    fos.write(copyResearchersXml.getBytes());
    fos.close();
    
    MyIsernXmlLoader loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile,
        validResearcherFile);
    
    FileReader file = new FileReader(System.getProperty(userDirectory) + 
        "/testFiles/commandLine1.txt");
    try {
      MyIsernCommandLineInterface.setBufferedReaderAndRun(file, loader);
    }
    catch (Exception error) {
      fail("Uncaught exception occured.");
    }
  }
}
